#============================================================
#    Control Hover Color
#============================================================
# - datetime: 2023-01-09 00:03:42
#============================================================
## Control 节点鼠标悬停颜色
##
##添加到 [Control] 节点下面，会在鼠标经过时设置下面的颜色
class_name ControlHoverColor
extends Node


@export
var enter_color : Color = Color.WHITE
@export
var click_color : Color = Color.WHITE


func _ready() -> void:
	var parent = get_parent()
	if parent is Control:
		var origin_color = parent.modulate
		parent.mouse_entered.connect(func(): parent.modulate = enter_color )
		parent.mouse_exited.connect(func(): parent.modulate = origin_color )
		
		parent.gui_input.connect(
			func(event):
				if InputUtil.is_click_left(event, true):
					parent.modulate = click_color
				elif InputUtil.is_click_left(event, false):
					var rect = parent.get_rect() as Rect2
					rect.position = Vector2(0, 0)
					if rect.has_point(parent.get_local_mouse_position()):
						# 鼠标在节点内部
						parent.modulate = enter_color
					else:
						parent.modulate = origin_color
		)
		
	else:
		printerr("父节点不是 Control 类型的节点！")
